/**
 * @(#)CodeEntityInvocationRelation.java, 2008-4-30.
 * Copyright 2008  Program Analysis Research Group(PARG),Institute of Software, Peking University.
 * All rights reserved.
 */
package edu.pku.sei.pgie.analyzer.internal.relation;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodInvocation;

import edu.pku.sei.pgie.analyzer.internal.CodeEntitySymbolTable;
import edu.pku.sei.pgie.analyzer.internal.VisitorWithContext;

/**
 * @author HeLi
 */
public class CodeEntityInvocationRelation extends AbstractCodeEntityRelation {

    class InvocationVisitor extends VisitorWithContext {
        List<String[]> container;

        public InvocationVisitor() {
            super(false);
            this.container = new ArrayList<String[]>();
        }

        public List<String[]> getResult() {
            return container;
        }

        public boolean visit(MethodInvocation node) {
            Expression expr = node.getExpression();
            if (expr != null) {
                ITypeBinding typeBinding = expr.resolveTypeBinding();
                if (typeBinding == null)
                    return true;
                String qualifiedName = typeBinding.getQualifiedName();
                String typeName = peekTypeFQN();
                if (typeName != null) {
                    container.add(new String[] {
                        typeName, qualifiedName
                    });
                }
            }
            return true;
        }
    }

    public String[][] collectCallBack(CompilationUnit root,
            CodeEntitySymbolTable symbolTable) throws Exception {
        InvocationVisitor visitor = new InvocationVisitor();
        root.accept(visitor);
        List<String[]> result = visitor.getResult();
        return result.toArray(new String[result.size()][]);
    }
}
